package cn.manyufun.myf.ui.comics.fragment;

import android.content.Context;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import java.text.SimpleDateFormat;

import butterknife.BindView;
import cn.manyufun.myf.R;
import cn.manyufun.myf.api.Api;
import cn.manyufun.myf.api.HostType;
import cn.manyufun.myf.app.AppConstant;
import cn.manyufun.myf.bean.BaseBean;
import cn.manyufun.myf.bean.ComicsDetailBean;
import cn.manyufun.myf.bean.ReplyBean;
import cn.manyufun.myf.common.base.BaseFragment;
import cn.manyufun.myf.common.baserx.RxSchedulers;
import cn.manyufun.myf.common.baserx.RxSubscriber;
import cn.manyufun.myf.common.commonutils.UserInfoUtils;
import cn.manyufun.myf.common.irecyclerview.universaladapter.ViewHolderHelper;
import cn.manyufun.myf.common.irecyclerview.universaladapter.recyclerview.CommonRecycleViewAdapter;
import cn.manyufun.myf.ui.comics.activity.ComicsDetailActivity;
import cn.manyufun.myf.ui.common.CommonListActivity;
import cn.manyufun.myf.ui.user.activity.LoginAct;

import static cn.manyufun.myf.common.commonutils.RequestBodyUtils.getLikeRequestBody;
import static cn.manyufun.myf.common.commonutils.TimeUtil.getfriendlyTime;

/**
 * Created by Xiao on 2018-02-07.
 */

public class ComicsCommentFragment extends BaseFragment {
    @BindView(R.id.tv_hot)
    TextView mTvHot;
    @BindView(R.id.lv_hot)
    RecyclerView mLvHot;
    @BindView(R.id.tv_new)
    TextView mTvNew;
    @BindView(R.id.lv_new)
    RecyclerView mLvNew;
    @BindView(R.id.tv_go_all)
    TextView mTvGo;
    private long mComicsId;
    final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd");
    private ComicsDetailBean mBean;

    public static ComicsCommentFragment newInstance(Bundle args) {
        ComicsCommentFragment fragment = new ComicsCommentFragment();
        if (args != null)
            fragment.setArguments(args);
        return fragment;
    }

    @Override
    protected int getLayoutResource() {
        return R.layout.comics_comment_list;
    }

    @Override
    public void initPresenter() {

    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        mBean = ((ComicsDetailActivity) getActivity()).getDetailBean();

    }

    @Override
    protected void initView() {
        Bundle args = getArguments();
        if (args != null)
            mComicsId = args.getLong("comicsid");
        mTvHot.setText("热门短评");
        mTvNew.setText("最新短评");
        mTvGo.setText("查看全部短评 >");
        CommonRecycleViewAdapter<ReplyBean> mHotAdapter = new CommonRecycleViewAdapter<ReplyBean>(getContext(), R.layout.layout_item_comment) {
            @Override
            public void convert(final ViewHolderHelper holder, final ReplyBean newsSummary) {
                holder.setText(R.id.tv_author_name, newsSummary.UserName);
                holder.setImageRoundUrl(R.id.iv_head, newsSummary.UserImageUrl);
                holder.setRating(R.id.rb_score, newsSummary.Score);
                holder.setText(R.id.tv_content, newsSummary.Content);
                holder.setText(R.id.tv_like, newsSummary.LikeNum + "");
                holder.setText(R.id.tv_publish_time, getfriendlyTime(newsSummary.ReplyTime.getTime()));
                holder.setOnClickListener(R.id.iv_like, new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        Api.getDefault(HostType.manyufun).like(getLikeRequestBody(1, newsSummary.ReplyId))
                                .compose(RxSchedulers.<BaseBean>io_main())
                                .subscribe(new RxSubscriber<BaseBean>(getContext(), false) {
                                    @Override
                                    public void onStart() {
                                        super.onStart();
                                    }

                                    @Override
                                    protected void _onNext(BaseBean posterBean) {
                                        if (posterBean.getSuccess().equals("1")) {
                                            showShortToast("点赞 + 1");
                                            holder.setText(R.id.tv_like, ++newsSummary.LikeNum + "");
                                        } else {
//                                            showShortToast(posterBean.getErrorMsg());
                                            showShortToast("已点赞");
                                        }

                                    }

                                    @Override
                                    protected void _onError(String message) {
                                        showShortToast(message);
                                    }
                                });
                    }
                });

            }
        };
        CommonRecycleViewAdapter<ReplyBean> mNewAdapter = new CommonRecycleViewAdapter<ReplyBean>(getContext(), R.layout.layout_item_comment) {
            @Override
            public void convert(final ViewHolderHelper holder, final ReplyBean newsSummary) {
                holder.setText(R.id.tv_author_name, newsSummary.UserName);
                holder.setImageRoundUrl(R.id.iv_head, newsSummary.UserImageUrl);
                holder.setRating(R.id.rb_score, newsSummary.Score);
                holder.setText(R.id.tv_content, newsSummary.Content);
                holder.setText(R.id.tv_like, newsSummary.LikeNum + "");
                holder.setText(R.id.tv_publish_time, getfriendlyTime(newsSummary.ReplyTime.getTime()));

                holder.setOnClickListener(R.id.iv_like, new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        if (!UserInfoUtils.getInstance().checkLogin()) {
                            Bundle bundle = new Bundle();
                            bundle.putString(LoginAct.LOGIN_FROM, ComicsDetailActivity.class.getSimpleName());
                            LoginAct.startAction(getContext(), bundle);
                            return;
                        }
                        Api.getDefault(HostType.manyufun).like(getLikeRequestBody(1, newsSummary.ReplyId))
                                .compose(RxSchedulers.<BaseBean>io_main())
                                .subscribe(new RxSubscriber<BaseBean>(getContext(), false) {
                                    @Override
                                    public void onStart() {
                                        super.onStart();
                                    }

                                    @Override
                                    protected void _onNext(BaseBean posterBean) {
                                        if (posterBean.getSuccess().equals("1")) {
                                            showShortToast("点赞 + 1");
                                            holder.setText(R.id.tv_like, ++newsSummary.LikeNum + "");
                                        } else {

//                                            showShortToast(posterBean.getErrorMsg());
                                            showShortToast("已点赞");
                                        }

                                    }

                                    @Override
                                    protected void _onError(String message) {
                                        showShortToast(message);
                                    }
                                });
                    }
                });
            }
        };

        LinearLayoutManager layoutHotManager = new LinearLayoutManager(getContext());
        layoutHotManager.setOrientation(LinearLayoutManager.VERTICAL);
        layoutHotManager.setAutoMeasureEnabled(true);
        LinearLayoutManager layoutNewManager = new LinearLayoutManager(getContext());
        layoutNewManager.setOrientation(LinearLayoutManager.VERTICAL);
        layoutNewManager.setAutoMeasureEnabled(true);
        // 设置布局管理器
        mLvHot.setLayoutManager(layoutHotManager);
        mLvNew.setLayoutManager(layoutNewManager);


        mLvHot.setAdapter(mHotAdapter);
        mLvNew.setAdapter(mNewAdapter);


        if (mBean.getHotReplyList() != null)
            mHotAdapter.replaceAll(mBean.getHotReplyList());
        else {
            mHotAdapter.clear();
        }
        if (mBean.getNewReplyList() != null)
            mNewAdapter.replaceAll(mBean.getNewReplyList());
        else {
            mNewAdapter.clear();
        }

        if (mBean.getHotReplyList() == null || mBean.getHotReplyList().size() == 0)
            mTvHot.setVisibility(View.GONE);
        else
            mTvHot.setVisibility(View.VISIBLE);
        if (mBean.getNewReplyList() == null || mBean.getNewReplyList().size() == 0)
            mTvNew.setVisibility(View.GONE);
        else
            mTvNew.setVisibility(View.VISIBLE);

        if (mBean.getNewReplyList() == null && mBean.getHotReplyList() == null || (mBean.getHotReplyList().size() == 0 && mBean.getNewReplyList().size() == 0))
            mTvGo.setText("还没有短评呢。");
        else {
            mTvGo.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Bundle bundle = new Bundle();
                    bundle.putInt(CommonListActivity.EXTRA_DATA_TYPE, AppConstant.TYPE_COMMENT);
                    bundle.putLong(CommonListActivity.EXTRA_DATA_OTHER, mBean.getComicId());
                    CommonListActivity.startAction(getContext(), bundle);
                }
            });
        }
    }
}
